package com.ruoyi.api.controller;

import com.ruoyi.result.ResultView;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: lvaitly
 * Time: 15:17
 * Desc: 对外字典
 */
@RestController
@RequestMapping("/api/dictdata")
public class SimpleDictDataController {
    @Autowired
    private ISysDictDataService dictDataService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @PostMapping("/{remark}")
    private ResultView getListByRemark(@PathVariable String remark){
        List<SysDictData> sysDictData=dictDataService.selectDictDataByRemark(remark);
        Map<String,List<SysDictData>> map=new HashMap<>();
        if(null!=sysDictData){
            sysDictData.forEach(c->{
                List<SysDictData> tmp=map.get(c.getDictType());
                if(null==tmp){
                    tmp=new ArrayList<>(1);
                }
                tmp.add(c);
                map.put(c.getDictType(),tmp);
            });
            sysDictData.clear();
        }
        return ResultView.ok(map);
    }

    @PostMapping("/list/type")
    private ResultView getListByType(@RequestBody SysDictData vo){
        if(null==vo.getDictType()){
            return ResultView.error(-1,"缺少参数");
        }
        return ResultView.ok(dictDataService.selectDictDataList(vo));
    }

    //获取一级分类
    @PostMapping("/topcategory/{remark}/list")
    private ResultView getTopCategoryByRemark(@PathVariable String remark){
        return ResultView.ok(dictTypeService.selectDictTypeByRemark(remark));
    }

    //二级分类
    @PostMapping("/secondcategory/list")
    private ResultView getSecondCategoryByType(@RequestBody SysDictData dictData){
        if(null==dictData.getDictType()){
            return ResultView.error(-1,"缺少参数");
        }
        return ResultView.ok(dictDataService.selectDictDataByTypeRemark(dictData));
    }
}
